<template>
  <div id="userLoginPage">
    <div class="login-container">
      <div class="login-card">
        <div class="card-header">
          <h2 class="title">HanCoder</h2>
          <div class="subtitle">AI 应用生成平台</div>
          <div class="desc">一句话轻松创建网站应用</div>
        </div>

        <a-form :model="formState" autocomplete="off" name="basic" @finish="handleSubmit" class="login-form">
          <a-form-item :rules="[{ required: true, message: '请输入账号' }]" name="userAccount">
            <a-input
              v-model:value="formState.userAccount"
              placeholder="请输入账号"
              size="large"
            >
              <template #prefix>
                <UserOutlined class="input-icon" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码长度不能小于 8 位' },
            ]"
            name="userPassword"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              placeholder="请输入密码"
              size="large"
            >
              <template #prefix>
                <LockOutlined class="input-icon" />
              </template>
            </a-input-password>
          </a-form-item>

          <div class="tips">
            没有账号？
            <RouterLink to="/user/register" class="register-link">立即注册</RouterLink>
          </div>

          <a-form-item>
            <a-button
              html-type="submit"
              type="primary"
              size="large"
              class="login-btn"
              :loading="loading"
            >
              登录
            </a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { userLogin } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

const router = useRouter()
const loginUserStore = useLoginUserStore()
const loading = ref(false)

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  loading.value = true
  try {
    const res = await userLogin(values)
    // 登录成功，把登录态保存到全局状态中
    if (res.data.code === 0 && res.data.data) {
      await loginUserStore.fetchLoginUser()
      message.success('登录成功')
      await router.push({
        path: '/',
        replace: true,
      })
    } else {
      message.error('登录失败，' + res.data.message)
    }
  } catch (error) {
    message.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
#userLoginPage {
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 25%, #334155 50%, #475569 100%);
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 科技感背景网格 */
#userLoginPage::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(rgba(0, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(138, 43, 226, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(138, 43, 226, 0.04) 1px, transparent 1px);
  background-size:
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  pointer-events: none;
  animation: gridFloat 20s ease-in-out infinite;
}

@keyframes gridFloat {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(2px, 2px); }
}

/* 动态光效 */
#userLoginPage::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 30% 40%, rgba(0, 255, 255, 0.08) 0%, transparent 50%),
    radial-gradient(circle at 70% 60%, rgba(138, 43, 226, 0.06) 0%, transparent 50%),
    radial-gradient(circle at 50% 80%, rgba(0, 255, 127, 0.04) 0%, transparent 50%);
  pointer-events: none;
  animation: lightPulse 8s ease-in-out infinite alternate;
}

@keyframes lightPulse {
  0% { opacity: 0.5; }
  100% { opacity: 1; }
}

.login-container {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 400px;
  padding: 20px;
}

.login-card {
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 255, 255, 0.2);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(circle at 20% 20%, rgba(0, 255, 255, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(138, 43, 226, 0.05) 0%, transparent 50%);
  pointer-events: none;
}

.card-header {
  text-align: center;
  margin-bottom: 32px;
  position: relative;
  z-index: 2;
}

.title {
  font-size: 36px;
  font-weight: 800;
  margin: 0 0 8px;
  background: linear-gradient(135deg, #00ffff 0%, #8a2be2 50%, #00ff7f 100%);
  background-size: 200% 200%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: titleShimmer 4s ease-in-out infinite;
}

@keyframes titleShimmer {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 200% 50%; }
}

.subtitle {
  font-size: 16px;
  color: #94a3b8;
  margin-bottom: 8px;
  font-weight: 500;
}

.desc {
  font-size: 14px;
  color: #64748b;
  opacity: 0.8;
}

.login-form {
  position: relative;
  z-index: 2;
}

.input-icon {
  color: #64748b;
}

:deep(.ant-input-affix-wrapper) {
  background: rgba(15, 23, 42, 0.8) !important;
  border: 1px solid rgba(0, 255, 255, 0.3) !important;
  border-radius: 12px;
  padding: 12px 16px;
  transition: all 0.3s ease;
}

:deep(.ant-input-affix-wrapper:focus-within) {
  border-color: rgba(0, 255, 255, 0.6) !important;
  box-shadow: 0 0 0 2px rgba(0, 255, 255, 0.2) !important;
}

:deep(.ant-input) {
  background: transparent !important;
  border: none !important;
  color: #e2e8f0 !important;
  font-size: 16px;
}

:deep(.ant-input::placeholder) {
  color: #64748b !important;
}

:deep(.ant-input-password) {
  background: transparent !important;
  color: #e2e8f0 !important;
}

.tips {
  text-align: center;
  color: #94a3b8;
  font-size: 14px;
  margin-bottom: 24px;
}

.register-link {
  color: #00ffff;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
}

.register-link:hover {
  color: #8a2be2;
  text-shadow: 0 0 8px rgba(0, 255, 255, 0.5);
}

.login-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #00ffff, #8a2be2) !important;
  border: none !important;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 8px 20px rgba(0, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 28px rgba(0, 255, 255, 0.4) !important;
}

.login-btn:active {
  transform: translateY(0);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    max-width: 320px;
    padding: 16px;
  }

  .login-card {
    padding: 32px 24px;
  }

  .title {
    font-size: 28px;
  }
}
</style>
